package cn.clementine.core.api.shell;

import cn.clementine.core.svc.WebShell;
import cn.clementine.core.vo.Result;
import cn.clementine.core.vo.TerminalResult;
import cn.clementine.core.vo.TerminalView;
import cn.dev33.satoken.annotation.SaCheckLogin;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
@SaCheckLogin
@RestController
@RequestMapping("/shell/")
public class ShellApi {
    @Resource
    WebShell webShell;
    @PostMapping("/delete_shell/{id}")
    public Result delete_shell(@PathVariable String id) {
        var terminal_map = webShell.getTerminals();
        terminal_map.keySet().forEach((e)->{
            if(e.equals(id)){
                terminal_map.get(e).stop();
                terminal_map.remove(e);
            }
        });
        return Result.ok("SUCCESS");
    }
    @GetMapping("/get_all_terminal")
    public Result getAllTerminal(){
        var terminal_map = webShell.getTerminals();
        TerminalResult tr = new TerminalResult();
        terminal_map.keySet().forEach((e)->{
            TerminalView terminalView = new TerminalView();
            terminalView.setTerminal_name(terminal_map.get(e).getTerminal_name());
            terminalView.setIs_alive(terminal_map.get(e).isAlive());
            terminalView.setTerminal_id(e);
            tr.getTerminalList().add(terminalView);
        });
        return Result.ok(tr);
    }
}
